package com.example.demo.Controller;

import com.example.demo.Service.userInfoService;
import com.example.demo.UserAuthentication;
import com.example.demo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;


/**
 * author 陈伯瑞
 * date 2020/5/17 0017 20:03
 * version 1.0
 **/
@Controller
public class userInfoController {
    /**
     *
     */
    @Autowired
    private userInfoService service;

    @PostMapping("/user/create")
    public ModelAndView create(@RequestParam("username") String username,
                               @RequestParam("password") String password,  Map<String, Object> map) {
        try {
         service.create(username,password);
        }catch (RuntimeException e){
            map.put("url", "/index");
            map.put("msg", "已存在用户！！！");
            return new ModelAndView("myerror", map);
        }
        UserInfo userInfo=new UserInfo(username,password,"user");
        Authentication auth = new UserAuthentication(userInfo, AuthorityUtils.createAuthorityList("user"));
        SecurityContextHolder.getContext().setAuthentication(auth);
        map.put("url", "/userinfo");
        map.put("msg", "成功添加用户");
        return new ModelAndView("success", map);
    }

/*
    @PostMapping("/user/login")
    public ModelAndView login(@RequestParam("username") String username,
                           @RequestParam("password") String password,  Map<String, Object> map) throws Exception {
        if(service.login(username,password)) {
            map.put("url", "/userinfo?username=" + username);
            map.put("msg", "登录成功");
            return new ModelAndView("success", map);

        }else {
            map.put("url", "/index");
            map.put("msg", "用户名/密码错误");
            return new ModelAndView("myerror", map);
        }
    }
*/

    @GetMapping("/index")
    public String adminLogin() {
        return "index";
    }
    @GetMapping("/update")
    public String adminupdate() {
        return "update";
    }

    @GetMapping("/userinfo")
    public ModelAndView userinfo(Map<String,Object> map) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        UserInfo userInfo= (UserInfo) authentication.getPrincipal();
        map.put("userInfo", userInfo);
        return  new ModelAndView("userinfo",map);
    }
    @PostMapping("/loginout")
    public String logout() {
        return "index";
    }
}
